package transactions.dq.jiesuan;

import dinamica.*;

/**
 * Reusable class to insert array of values into DB.<br>
 * <br>
 * <br>
 * Creation date: 5/03/2004<br>
 * Last Update: 5/03/2004<br>
 * (c) 2004 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * 
 * @author Martin Cordova (dinamica@martincordova.com)
 * */
public class InsertDetail extends dinamica.GenericTransaction {

	/**
	 * Insert array in DB
	 * 
	 * @param sql
	 *            SQL template for the INSERT statement, may vary according to
	 *            the context (a new user or an existing user).
	 * @param colName
	 *            parameter name for the array of values
	 * @throws Throwable
	 */
	public void save(String sql) throws Throwable {

		// get db channel
		Db db = getDb();

		// create recordset to hold array
		Recordset detail = new Recordset();

		String colName1 = "house_number";
		String colName2 = "house_area";
		String colName3 = "house_level";
		String colName4 = "house_price";
		String colName5 = "house_fee";

		detail.append(colName1, java.sql.Types.INTEGER);
		detail.append(colName2, java.sql.Types.DOUBLE);
		detail.append(colName3, java.sql.Types.INTEGER);
		detail.append(colName4, java.sql.Types.DOUBLE);
		detail.append(colName5, java.sql.Types.DOUBLE);

		System.out.println("11");
		// get array
		String v1[] = getRequest().getParameterValues(colName1);
		String v2[] = getRequest().getParameterValues(colName2);
		String v3[] = getRequest().getParameterValues(colName3);
		String v4[] = getRequest().getParameterValues(colName4);
		String v5[] = getRequest().getParameterValues(colName5);// 金额

		// read values if not empty
		if (v5 != null) {
			for (int i = 0; i < v5.length; i++) {
				// 判断金额是否为空
				if ((v1[i].trim().length()>0) && (v2[i].trim().length()>0) && (v3[i].trim().length()>0) && (v4[i].trim().length()>0) && (v5[i].trim().length()>0)) {
					// add to recordset
					detail.addNew();
					detail.setValue(colName1, new Integer(v1[i]));
					detail.setValue(colName2, new Double(v2[i]));
					detail.setValue(colName3, new Integer(v3[i]));
					detail.setValue(colName4, new Double(v4[i]));
					detail.setValue(colName5, new Double(v5[i]));

					// generate sql
					String cmd = getSQL(sql, detail);

					// add to batch
					db.addBatchCommand(cmd);
				}
			}

			// insert all records in batch
			db.exec();
		}

	}

}
